﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Tongiao.Entity;
using Tongiao.Manager;
using Tongiao.ManagerBase;

public partial class admin_Location_LocationList : System.Web.UI.UserControl
{
    Guid Id = Guid.Empty;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadLoaiDao();
            LoadAllData();
        }
    }

    void LoadLoaiDao()
    {
        DataTable dtLoaiDao = Tg_LoaiDaoManager.CreateInstant().SelectAll();
        ddlLoaiDao.DataSource = dtLoaiDao;
        ddlLoaiDao.DataTextField = "TenLoaiDao";
        ddlLoaiDao.DataValueField = "MaLoaiDao";
        ddlLoaiDao.DataBind();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Id == Guid.Empty)
            Insert();
        else
            Update();

        LoadAllData();
    }

    void Insert()
    {
        Tg_LocationEntity objLoca = new Tg_LocationEntity();
        objLoca.LocationName = txtLocation.Text;
        objLoca.IdLoaiDao = ddlLoaiDao.SelectedValue;
        objLoca.CreatedBy = Profile.UserName;
        objLoca.CreatedDate = DateTime.Now;

        Tg_LocationManager.CreateInstant().Insert(objLoca);
        

    }

    void LoadAllData()
    {
        DataTable dtLocation = Tg_LocationManager.CreateInstant().getAllListLocation();
        if (dtLocation.Rows.Count > 0)
        {
            rep.DataSource = dtLocation;
            rep.DataBind();
        }
    }

    void Update()
    {
        Tg_LocationEntity objLoca = new Tg_LocationEntity();
        objLoca.Id = Guid.Empty;
        objLoca.LocationName = txtLocation.Text;
        objLoca.IdLoaiDao = ddlLoaiDao.SelectedValue;
        objLoca.UpdatedBy = Profile.UserName;
        objLoca.UpdatedDate = DateTime.Now;

        Tg_LocationManager.CreateInstant().Update(objLoca);
    }

    protected void rep_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        
        Tg_LocationEntity objLoca = new Tg_LocationEntity();
        switch (e.CommandName)
        {
            case "edit":
                {
                    DataTable dt = Tg_LocationManager.CreateInstant().SelectById(new Guid (e.CommandArgument.ToString()));
                    if (dt.Rows.Count > 0)
                    {
                        Id = new Guid(dt.Rows[0]["Id"].ToString());
                        objLoca.Id = new Guid(dt.Rows[0]["Id"].ToString());
                        txtLocation.Text = dt.Rows[0][1].ToString();
                        ddlLoaiDao.SelectedValue = dt.Rows[0][2].ToString(); 
                        
                    }
                    break;
                }
                
            case "del":
                {
                    Tg_LocationManager.CreateInstant().DeleteById(new Guid(e.CommandArgument.ToString()));
                    LoadAllData();
                    break;
                }
                
        }
    }
}
